package com.atjava.webspider.gui.data;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.apache.commons.lang.StringUtils;

import com.atjava.webspider.consts.Const;
import com.atjava.webspider.database.ConnFactory;
import com.atjava.webspider.database.support.MySQLDataSource;
import com.atjava.webspider.database.support.MySQLManager;
import com.atjava.webspider.gui.table.DefaultTableModel;
import com.atjava.webspider.logic.CommonLogic;
import com.atjava.webspider.logic.ConfigLogic;
import com.atjava.webspider.logic.DatabaseLogic;
import com.atjava.webspider.logic.VarLogic;
import com.atjava.webspider.model.DatabaseConfig;
import com.atjava.webspider.model.DbConfig;
import com.atjava.webspider.util.DateUtil;
import com.atjava.webspider.util.DialogUtil;
import com.atjava.webspider.util.FileSystemUtil;
import com.atjava.webspider.util.TableUtil;

public class DataIntoGui extends JDialog {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JButton jButtonInto = null;
	private JTextArea jTextAreaData = null;
	
	private JScrollPane jScrollPane = null;

	private JScrollPane jScrollPaneVar = null;

	private DefaultTableModel varModel = null;
	
	private JTable jTableVar = null;

	private JPanel jPanelVar = null;

	private JPanel jPaneSql = null;

	private JPanel jPanelConfig = null;

	private JButton jButtonCreateSql = null;

	private JScrollPane jScrollPaneDbTable = null;

	private DefaultTableModel dbTableModel = null;
	private JTable jTableDbTable = null;

	private JTextField jTextFieldSqlPath = null;

	private JButton jButtonBrowser = null;

	private JLabel jLabel = null;

	private JTextField jTextFieldCharset = null;

	private JLabel jLabel1 = null;

	private JLabel jLabel2 = null;

	private JLabel jLabel3 = null;

	private JLabel jLabel4 = null;

	private JTextField jTextFieldUser = null;

	private JPasswordField jTextFieldPasswd = null;

	private JTextField jTextFieldConStr = null;

	private JButton jButtonConnect = null;

	private JLabel jLabel5 = null;

	private JComboBox jComboBoxDbType = null;

	private JLabel jLabel6 = null;

	private JList jListTables = null;
	private JScrollPane jScrollPaneTables = null;
	private JButton jButtonDiscnnt = null;
	private DatabaseLogic dbLogic = null;
	private JLabel jLabel7 = null;
	private JLabel jLabelDataDir = null;
	private JButton jButtonSelectData = null;
	private JButton jButtonOpen = null;
	private JLabel jLabel12 = null;
	private JComboBox jComboBoxConfigName = null;
	private JButton jButtonSaveCfg = null;
	private JButton jButtonDelCfg = null;
	private JLabel jLabelDataNum = null;
	private JButton jButtonSaveInto = null;
	private int dataNum = 0;
	private JLabel jLabel8 = null;
	private JTextField jTextFieldDbCharset = null;
	/**
	 * @param owner
	 */
	public DataIntoGui(Frame owner,boolean modal) {
		super(owner,modal);
		this.dbLogic = new DatabaseLogic();
		initialize();
		setTextValue();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setBounds(new Rectangle(0, 0, 670, 576));
		this.setResizable(false);
		this.setTitle("数据入库 ");
		this.setLocationRelativeTo(getOwner());
		this.setContentPane(getJContentPane());
	}
	
	private void setTextValue(){
		
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabelDataNum = new JLabel();
			jLabelDataNum.setBounds(new Rectangle(484, 6, 70, 23));
			jLabelDataNum.setBorder(BorderFactory.createLineBorder(SystemColor.controlShadow, 1));
			jLabelDataNum.setText("共 0 个");
			jLabel12 = new JLabel();
			jLabel12.setBounds(new Rectangle(7, 36, 80, 23));
			jLabel12.setText("配置名称：");
			jLabelDataDir = new JLabel();
			jLabelDataDir.setBounds(new Rectangle(88, 6, 389, 23));
			jLabelDataDir.setForeground(new Color(204, 51, 0));
			jLabelDataDir.setBorder(BorderFactory.createLineBorder(SystemColor.controlShadow, 1));
			jLabelDataDir.setText("");
			jLabel7 = new JLabel();
			jLabel7.setBounds(new Rectangle(7, 6, 80, 23));
			jLabel7.setText("数据文件夹：");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJPanelVar(), null);
			jContentPane.add(getJPaneSql(), null);
			jContentPane.add(getJPanelConfig(), null);
			jContentPane.add(getJButtonInto(), null);
			jContentPane.add(jLabel5, null);
			jContentPane.add(getJComboBoxDbType(), null);
			jContentPane.add(jLabel7, null);
			jContentPane.add(jLabelDataDir, null);
			jContentPane.add(getJButtonSelectData(), null);
			jContentPane.add(getJButtonOpen(), null);
			jContentPane.add(jLabel12, null);
			jContentPane.add(getJComboBoxConfigName(), null);
			jContentPane.add(getJButtonSaveCfg(), null);
			jContentPane.add(getJButtonDelCfg(), null);
			jContentPane.add(jLabelDataNum, null);
			jContentPane.add(getJButtonSaveInto(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jButtonInto	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonInto() {
		if (jButtonInto == null) {
			jButtonInto = new JButton();
			jButtonInto.setMargin(new Insets(2, 5, 2, 5));
			jButtonInto.setText("数据入库");
			jButtonInto.setBounds(new Rectangle(512, 517, 70, 23));
			jButtonInto.addMouseListener(new MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					DataIntoGui.this.setVisible(false);
				}
			});
		}
		return jButtonInto;
	}

	/**
	 * This method initializes jTextAreaData	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getJTextFieldData() {
		if (jTextAreaData == null) {
			jTextAreaData = new JTextArea();
			jTextAreaData.setForeground(new Color(0, 0, 204));
			jTextAreaData.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		}
		return jTextAreaData;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(7, 18, 638, 65));
			jScrollPane.setViewportView(getJTextFieldData());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jScrollPaneVar	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPaneVar() {
		if (jScrollPaneVar == null) {
			jScrollPaneVar = new JScrollPane();
			jScrollPaneVar.setBounds(new Rectangle(7, 18, 638, 50));
			jScrollPaneVar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
			jScrollPaneVar.setViewportView(getJTableVar());
		}
		return jScrollPaneVar;
	}

	/**
	 * This method initializes jTableVar	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTableVar() {
		if (jTableVar == null) {
			//表头
			String [] colName = {};
			varModel = new DefaultTableModel(colName);
			jTableVar = new JTable(varModel);
			jTableVar.setRowHeight(25);
			jTableVar.setEnabled(false);
			jTableVar.setForeground(new Color(0, 0, 204));
			jTableVar.setFont(new Font("Courier New", Font.BOLD, 12));
			TableUtil.setTableAlign(jTableVar, JLabel.CENTER);
			jTableVar.setBounds(new Rectangle(7, 18, 638, 50));
		}
		return jTableVar;
	}

	/**
	 * This method initializes jPanelVar	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelVar() {
		if (jPanelVar == null) {
			jPanelVar = new JPanel();
			jPanelVar.setLayout(null);
			jPanelVar.setBounds(new Rectangle(6, 61, 652, 76));
			jPanelVar.setBorder(BorderFactory.createTitledBorder(null, " 可用变量 "));
			jPanelVar.add(getJScrollPaneVar(), null);
		}
		return jPanelVar;
	}

	/**
	 * This method initializes jPaneSql	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPaneSql() {
		if (jPaneSql == null) {
			jLabel6 = new JLabel();
			jLabel6.setBounds(new Rectangle(180, 86, 70, 23));
			jLabel6.setText("保存位置：");
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(7, 86, 70, 23));
			jLabel1.setText("文件编码：");
			jPaneSql = new JPanel();
			jPaneSql.setLayout(null);
			jPaneSql.setBorder(BorderFactory.createTitledBorder(null, " SQL模板 "));
			jPaneSql.setBounds(new Rectangle(6, 137, 652, 116));
			jPaneSql.add(getJScrollPane(), null);
			jPaneSql.add(getJButtonCreateSql(), null);
			jPaneSql.add(getJTextFieldSqlPath(), null);
			jPaneSql.add(getJButtonBrowser(), null);
			jPaneSql.add(getJTextFieldCharset(), null);
			jPaneSql.add(jLabel1, null);
			jPaneSql.add(jLabel6, null);
			
		}
		return jPaneSql;
	}

	/**
	 * This method initializes jPanelConfig	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelConfig() {
		if (jPanelConfig == null) {
			jLabel8 = new JLabel();
			jLabel8.setBounds(new Rectangle(195, 50, 55, 23));
			jLabel8.setText("字符集：");
			jLabel5 = new JLabel();
			jLabel5.setText("数据库类型：");
			jLabel5.setBounds(new Rectangle(458, 36, 80, 23));
			jLabel4 = new JLabel();
			jLabel4.setBounds(new Rectangle(10, 82, 52, 23));
			jLabel4.setText("密码：");
			jLabel3 = new JLabel();
			jLabel3.setBounds(new Rectangle(10, 50, 59, 23));
			jLabel3.setText("用户名：");
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(10, 18, 80, 23));
			jLabel2.setText("数据库链接：");
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(10, 115, 59, 23));
			jLabel.setText("导入表：");
			jPanelConfig = new JPanel();
			jPanelConfig.setLayout(null);
			jPanelConfig.setBorder(BorderFactory.createTitledBorder(null, " 入库配置 "));
			jPanelConfig.setBounds(new Rectangle(7, 253, 652, 260));
			jPanelConfig.add(getJScrollPaneDbTable(), null);
			jPanelConfig.add(jLabel, null);
			jPanelConfig.add(jLabel2, null);
			jPanelConfig.add(jLabel3, null);
			jPanelConfig.add(jLabel4, null);
			jPanelConfig.add(getJTextFieldUser(), null);
			jPanelConfig.add(getJTextFieldPasswd(), null);
			jPanelConfig.add(getJComboBoxConStr(), null);
			jPanelConfig.add(getJButtonConnect(), null);
			jPanelConfig.add(getJScrollPaneTables(), null);
			jPanelConfig.add(getJButtonDiscnnt(), null);
			jPanelConfig.add(jLabel8, null);
			jPanelConfig.add(getJTextFieldDbCharset(), null);
		}
		return jPanelConfig;
	}

	/**
	 * This method initializes jButtonCreateSql	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonCreateSql() {
		if (jButtonCreateSql == null) {
			jButtonCreateSql = new JButton();
			jButtonCreateSql.setBounds(new Rectangle(569, 86, 75, 23));
			jButtonCreateSql.setMargin(new Insets(2, 5, 2, 5));
			jButtonCreateSql.setText("生成SQL");
			jButtonCreateSql.addMouseListener(new MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					String savePath = jTextFieldSqlPath.getText();
					String dataFolder = jLabelDataDir.getText();
					String charset = jTextFieldCharset.getText();
					String fileName = DateUtil.formatDateTime(new Date());
					FileSystemUtil fsu = new FileSystemUtil();
					File sqlFile = new File(savePath + fileName + ".sql");
					try {
						//生成SQL语句
						String sql = dbLogic.createSql
							(dataFolder, jTextAreaData.getText(),DataIntoGui.this.dataNum,charset);
						fsu.writeText(sql, sqlFile,charset);
						DialogUtil.showMessage(DataIntoGui.this, "成功输出到文件！\r\n" + sqlFile.getAbsolutePath());
					} catch (Exception e1) {
						e1.printStackTrace();
						DialogUtil.showError(DataIntoGui.this, "输出到文件错误！");
					}
					
				}
			});
		}
		return jButtonCreateSql;
	}

	/**
	 * This method initializes jScrollPaneDbTable	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPaneDbTable() {
		if (jScrollPaneDbTable == null) {
			jScrollPaneDbTable = new JScrollPane();
			jScrollPaneDbTable.setBounds(new Rectangle(341, 18, 303, 236));
			jScrollPaneDbTable.setViewportView(getJTableDbTable());
		}
		return jScrollPaneDbTable;
	}

	/**
	 * This method initializes jTableDbTable	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTableDbTable() {
		if (jTableDbTable == null) {
			String [] colName = {"字段","导入值"};
			dbTableModel = new DefaultTableModel(colName);
			dbTableModel.setEditColumns(new int[]{1});
			jTableDbTable = new JTable(dbTableModel);
			jTableDbTable.setRowHeight(25);
			TableUtil.setTableAlign(jTableDbTable, JLabel.CENTER);
		}
		return jTableDbTable;
	}

	/**
	 * This method initializes jTextFieldSqlPath	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldSqlPath() {
		if (jTextFieldSqlPath == null) {
			jTextFieldSqlPath = new JTextField();
			jTextFieldSqlPath.setBounds(new Rectangle(250, 86, 250, 23));
		}
		return jTextFieldSqlPath;
	}

	/**
	 * This method initializes jButtonBrowser	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonBrowser() {
		if (jButtonBrowser == null) {
			jButtonBrowser = new JButton();
			jButtonBrowser.setBounds(new Rectangle(510, 86, 50, 23));
			jButtonBrowser.setMargin(new Insets(2, 5, 2, 5));
			jButtonBrowser.setText("浏览");
			jButtonBrowser.addMouseListener(new MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					JFileChooser jc = new JFileChooser
					(new File(Const.APP_PATH));
					jc.setBackground(Color.WHITE);
					jc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
					jc.showOpenDialog(DataIntoGui.this);
					File f = jc.getSelectedFile();
					if( f == null )return ;
					DataIntoGui.this.jTextFieldSqlPath.setText(f.getAbsolutePath());
				}
			});
		}
		return jButtonBrowser;
	}

	/**
	 * This method initializes jTextFieldCharset	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldCharset() {
		if (jTextFieldCharset == null) {
			jTextFieldCharset = new JTextField();
			jTextFieldCharset.setBounds(new Rectangle(78, 86, 81, 23));
			jTextFieldCharset.setText("GB2312");
		}
		return jTextFieldCharset;
	}

	/**
	 * This method initializes jTextFieldUser	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldUser() {
		if (jTextFieldUser == null) {
			jTextFieldUser = new JTextField();
			jTextFieldUser.setBounds(new Rectangle(90, 50, 92, 23));
		}
		return jTextFieldUser;
	}

	/**
	 * This method initializes jTextFieldPasswd	
	 * 	
	 * @return javax.swing.JPasswordField	
	 */
	private JPasswordField getJTextFieldPasswd() {
		if (jTextFieldPasswd == null) {
			jTextFieldPasswd = new JPasswordField();
			jTextFieldPasswd.setBounds(new Rectangle(90, 82, 91, 23));
		}
		return jTextFieldPasswd;
	}

	/**
	 * This method initializes jComboBoxConStr	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJComboBoxConStr() {
		if (jTextFieldConStr == null) {
			jTextFieldConStr = new JTextField();
			jTextFieldConStr.setBounds(new Rectangle(90, 18, 240, 23));
		}
		return jTextFieldConStr;
	}

	/**
	 * This method initializes jButtonConnect	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonConnect() {
		if (jButtonConnect == null) {
			jButtonConnect = new JButton();
			jButtonConnect.setBounds(new Rectangle(195, 82, 60, 23));
			jButtonConnect.setMargin(new Insets(2, 5, 2, 5));
			jButtonConnect.setText("连接");
			jButtonConnect.addMouseListener(new MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					DataIntoGui.this.setCursor(new Cursor(Cursor.WAIT_CURSOR));
					try {
						String dbPath = jTextFieldConStr.getText();
						String username = jTextFieldUser.getText();
						String passwd = new String(jTextFieldPasswd.getPassword());
						MySQLDataSource ds = 
							new MySQLDataSource(dbPath,username,passwd);
						MySQLManager sqlmgr = 
							new MySQLManager(ConnFactory.createConnection(ds));
						List<String> tables = sqlmgr.getTables();
						jListTables.setListData(tables.toArray());
						jButtonConnect.setEnabled(false);
						jButtonDiscnnt.setEnabled(true);
					} catch (Exception e2) {
						e2.printStackTrace();
						DialogUtil.showError(DataIntoGui.this, "连接数据库失败！");
					} finally{
						DataIntoGui.this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
					}
				}
			});
		}
		return jButtonConnect;
	}

	/**
	 * This method initializes jComboBoxDbType	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBoxDbType() {
		if (jComboBoxDbType == null) {
			String [] dbType = new String[]{"Mysql","Oracle","Access"}; 
			jComboBoxDbType = new JComboBox(dbType);
			jComboBoxDbType.setBounds(new Rectangle(537, 36, 120, 23));
		}
		return jComboBoxDbType;
	}

	/**
	 * This method initializes jListTables	
	 * 	
	 * @return javax.swing.JList	
	 */
	private JList getJListTables() {
		if (jListTables == null) {
			jListTables = new JList();
			jListTables.setFont(new Font("Courier New", Font.BOLD, 13));
			jListTables.addListSelectionListener(new ListSelectionListener(){
				public void valueChanged(ListSelectionEvent e) {
					DataIntoGui.this.setCursor(new Cursor(Cursor.WAIT_CURSOR));
					try {
						String table = (String)jListTables.getSelectedValue();
						MySQLManager sqlmgr = 
							new MySQLManager(ConnFactory.getConnection());
						List<String> fields = sqlmgr.getTableFields(table);
						dbTableModel.removeAllData();
						for (int i = 0; i < fields.size(); i++) {
							String [] row = {fields.get(i),"0"};
							dbTableModel.addData(row);
						}
						
					} catch (Exception e2) {
						e2.printStackTrace();
						DialogUtil.showError(DataIntoGui.this, "连接数据库失败！");
					} finally{
						DataIntoGui.this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
					}
				}
			});
		}
		return jListTables;
	}

	/**
	 * This method initializes jScrollPaneTables	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPaneTables() {
		if (jScrollPaneTables == null) {
			jScrollPaneTables = new JScrollPane();
			jScrollPaneTables.setBounds(new Rectangle(90, 115, 239, 140));
			jScrollPaneTables.setViewportView(getJListTables());
		}
		return jScrollPaneTables;
	}

	/**
	 * This method initializes jButtonDiscnnt	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonDiscnnt() {
		if (jButtonDiscnnt == null) {
			jButtonDiscnnt = new JButton();
			jButtonDiscnnt.setBounds(new Rectangle(269, 82, 60, 23));
			jButtonDiscnnt.setMargin(new Insets(2, 5, 2, 5));
			jButtonDiscnnt.setEnabled(false);
			jButtonDiscnnt.setText("断开");
		}
		return jButtonDiscnnt;
	}

	/**
	 * This method initializes jButtonSelectData	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonSelectData() {
		if (jButtonSelectData == null) {
			jButtonSelectData = new JButton();
			jButtonSelectData.setBounds(new Rectangle(561, 6, 45, 23));
			jButtonSelectData.setText("选择");
			jButtonSelectData.setMargin(new Insets(2, 5, 2, 5));
			jButtonSelectData.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String defPath = DataIntoGui.this.jLabelDataDir.getText();
					File selectDir;
					if(StringUtils.isNotBlank(defPath)){
						selectDir = new File(defPath).getParentFile();
					}else{
						selectDir = new File(Const.APP_PATH);
					}
					JFileChooser jc = new JFileChooser(selectDir);
					jc.setBackground(Color.WHITE);
					jc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
					jc.showOpenDialog(DataIntoGui.this);
					File f = jc.getSelectedFile();
					if( f == null )return ;
					DataIntoGui.this.setCursor(new Cursor(Cursor.WAIT_CURSOR));
					//设置文件夹路径
					DataIntoGui.this.jLabelDataDir.setText(f.getAbsolutePath());
					//设置数据量
					DataIntoGui.this.dataNum = VarLogic.getVarCountFromFolder(f.getAbsolutePath());
					DataIntoGui.this.jLabelDataNum.setText("共 " + DataIntoGui.this.dataNum + " 个");
					Set<String> varSet = VarLogic.getVarNamesFromFolder(f.getAbsolutePath());
					if(varSet.size() > 0){
						//数据量变量
						varSet.add(Const.DATA_NUM_VAR_NAME);
						//加载变量
						varModel.removeHeader();
						varModel.removeAllData();
						varModel.setHeader(VarLogic.getAvailableVarHeader(varSet.size()));
						varModel.addData(varSet.toArray(new String[0]));
					}
					DataIntoGui.this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
				}
			});
		}
		return jButtonSelectData;
	}

	/**
	 * This method initializes jButtonOpen	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonOpen() {
		if (jButtonOpen == null) {
			jButtonOpen = new JButton();
			jButtonOpen.setBounds(new Rectangle(612, 6, 45, 23));
			jButtonOpen.setMargin(new Insets(2, 5, 2, 5));
			jButtonOpen.setText("打开");
			jButtonOpen.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String dir = jLabelDataDir.getText();
					if(StringUtils.isBlank(dir))
						return ;
					File f = new File(dir);
					if(f.exists()){
						try {
							Runtime.getRuntime().exec("explorer " + f.getAbsolutePath());
						} catch (Exception ex) {
							ex.printStackTrace();
							DialogUtil.showError
							(DataIntoGui.this, "打开文件夹 [ " + f.getAbsolutePath() + " ] 失败！");
						}
					}
				}
			});
		}
		return jButtonOpen;
	}

	/**
	 * This method initializes jComboBoxConfigName	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBoxConfigName() {
		if (jComboBoxConfigName == null) {
			try {
				List<DatabaseConfig> configs = ConfigLogic.getDbConfig().getConfigs();
				if(configs != null){
					String [] configsNm = new String[configs.size()];
					for (int i = 0; i < configs.size(); i++) {
						configsNm [i] = configs.get(i).getName();
					}
					jComboBoxConfigName = new JComboBox(configsNm);
				}else{
					jComboBoxConfigName = new JComboBox();
				}
				jComboBoxConfigName.setEditable(true);
				jComboBoxConfigName.setBounds(new Rectangle(88, 36, 190, 23));
			} catch (Exception e) {
				e.printStackTrace();
				DialogUtil.showError(this, "获取数据库配置列表失败！");
			}
		}
		return jComboBoxConfigName;
	}

	/**
	 * This method initializes jButtonSaveCfg	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonSaveCfg() {
		if (jButtonSaveCfg == null) {
			jButtonSaveCfg = new JButton();
			jButtonSaveCfg.setMargin(new Insets(2, 5, 2, 5));
			jButtonSaveCfg.setText("载入配置");
			jButtonSaveCfg.setBounds(new Rectangle(284, 36, 70, 23));
			jButtonSaveCfg.addMouseListener(new MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try{
						String dbCfgName = (String)DataIntoGui.this.jComboBoxConfigName.getSelectedItem();
						List<DatabaseConfig> configs = ConfigLogic.getDbConfig().getConfigs();
						DatabaseConfig dbCfg = (DatabaseConfig)CommonLogic.getByName(configs, dbCfgName);
						
						if(dbCfg == null){
							DialogUtil.showError(DataIntoGui.this, "配置不存在！");
							return ;
						}
						
						if(StringUtils.isNotBlank(dbCfg.getSqlPath()))
							jTextFieldSqlPath.setText(dbCfg.getSqlPath());
						
						if(StringUtils.isNotBlank(dbCfg.getSqlEncode()))
							jTextFieldCharset.setText(dbCfg.getSqlEncode());
						
						if(StringUtils.isNotBlank(dbCfg.getSqlTmplt()))
							jTextAreaData.setText(dbCfg.getSqlTmplt());
						
						if(StringUtils.isNotBlank(dbCfg.getUrl()))
							jTextFieldConStr.setText(dbCfg.getUrl());
						
						if(StringUtils.isNotBlank(dbCfg.getUsername()))
							jTextFieldUser.setText(dbCfg.getUsername());
						
						if(StringUtils.isNotBlank(dbCfg.getPassword()))
							jTextFieldPasswd.setText(dbCfg.getPassword());
					} catch (Exception ex) {
						ex.printStackTrace();
						DialogUtil.showError(DataIntoGui.this, "载入配置失败！");
					}
				}
			});
		}
		return jButtonSaveCfg;
	}

	/**
	 * This method initializes jButtonDelCfg	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonDelCfg() {
		if (jButtonDelCfg == null) {
			jButtonDelCfg = new JButton();
			jButtonDelCfg.setMargin(new Insets(2, 5, 2, 5));
			jButtonDelCfg.setText("删除配置");
			jButtonDelCfg.setBounds(new Rectangle(359, 36, 70, 23));
			jButtonDelCfg.addMouseListener(new MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try{
						String dbCfgName = (String)DataIntoGui.this.jComboBoxConfigName.getSelectedItem();
						int index = DataIntoGui.this.jComboBoxConfigName.getSelectedIndex();
						List<DatabaseConfig> configs = ConfigLogic.getDbConfig().getConfigs();
						CommonLogic.removeByName(configs, dbCfgName);
						ConfigLogic.saveDbConfig();
						DataIntoGui.this.jComboBoxConfigName.removeItemAt(index);
						DataIntoGui.this.jComboBoxConfigName.updateUI();
						//DataIntoGui.this.jComboBoxConfigName.repaint();
					} catch (Exception ex) {
						ex.printStackTrace();
						DialogUtil.showError(DataIntoGui.this, "删除配置失败！");
					}
				}
			});
		}
		return jButtonDelCfg;
	}

	/**
	 * This method initializes jButtonSaveInto	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonSaveInto() {
		if (jButtonSaveInto == null) {
			jButtonSaveInto = new JButton();
			jButtonSaveInto.setMargin(new Insets(2, 5, 2, 5));
			jButtonSaveInto.setBounds(new Rectangle(589, 517, 70, 23));
			jButtonSaveInto.setText("保存配置");
			jButtonSaveInto.addMouseListener(new MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					String cfgName = (String)jComboBoxConfigName.getSelectedItem();
					String sqlTmplt = jTextAreaData.getText();
					String sqlPath = jTextFieldSqlPath.getText();
					String charset = jTextFieldCharset.getText();
					String url = jTextFieldConStr.getText();
					String user = jTextFieldUser.getText();
					String pass = new String(jTextFieldPasswd.getPassword());
					
					DbConfig dbcfgs = ConfigLogic.getDbConfig();
					if(dbcfgs.getConfigs() == null){
						dbcfgs.setConfigs(new ArrayList<DatabaseConfig>());
					}
					
					DatabaseConfig dbCfg = (DatabaseConfig)CommonLogic.getByName(dbcfgs.getConfigs(), cfgName);
					
					if(dbCfg == null){
						dbCfg = new DatabaseConfig();
						dbcfgs.getConfigs().add(dbCfg);
					}
					
					dbCfg.setName(cfgName);
					dbCfg.setSqlTmplt(sqlTmplt);
					dbCfg.setSqlPath(sqlPath);
					dbCfg.setSqlEncode(charset);
					dbCfg.setUrl(url);
					dbCfg.setUsername(user);
					dbCfg.setPassword(pass);

					ConfigLogic.saveDbConfig();
					DataIntoGui.this.setVisible(false);
				}
			});
		}
		return jButtonSaveInto;
	}

	/**
	 * This method initializes jTextFieldDbCharset	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldDbCharset() {
		if (jTextFieldDbCharset == null) {
			jTextFieldDbCharset = new JTextField();
			jTextFieldDbCharset.setBounds(new Rectangle(251, 50, 79, 22));
		}
		return jTextFieldDbCharset;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
